package com.jintian.smart.kernel.module.config;

import com.jintian.smart.kernel.core.annotation.beetl.BeetlFunction;
import lombok.extern.slf4j.Slf4j;
import org.beetl.core.Format;
import org.beetl.core.Function;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.core.type.filter.AssignableTypeFilter;

import java.util.Set;

@Slf4j
public class BeetlTemplateElementScanner extends ClassPathBeanDefinitionScanner {
    public BeetlTemplateElementScanner(BeanDefinitionRegistry registry) {
        super(registry);
        // this.setBeanNameGenerator(new BeanNameGenerator() {
        // @Override
        // public String generateBeanName(BeanDefinition definition,
        // BeanDefinitionRegistry registry) {
        // return AnnotationBeanNameGenerator.INSTANCE.generateBeanName(definition,
        // registry);
        // }
        // });
    }

    public void registerDefaultFilters() {
        this.addIncludeFilter(new AnnotationTypeFilter(BeetlFunction.class));
        this.addIncludeFilter(new AssignableTypeFilter(Function.class));
        this.addIncludeFilter(new AssignableTypeFilter(Format.class));
    }

    public Set<BeanDefinitionHolder> doScan(String... basePackages) {
        Set<BeanDefinitionHolder> beanDefinitions = super.doScan(basePackages);
        for (BeanDefinitionHolder holder : beanDefinitions) {
            log.info("find beetl element  {} ", holder.getBeanDefinition().getBeanClassName());
        }
        return beanDefinitions;
    }
}
